package avicit.bdp.dgs.metadata.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.service.dto.BdpColumnDTO;
import avicit.bdp.dgs.metadata.dao.MetadataTableColumnDao;
import avicit.bdp.dgs.metadata.vo.MetadataTableColumnVo;
import avicit.platform6.core.exception.BusinessException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 数据表元数据表列表service
 *
 * @author mayanj
 */
@Service
public class MetadataTableColumnService extends BaseService<MetadataTableColumnDao, BdpColumnDTO> {

  public Page<BdpColumnDTO> selectPageList(
      String tableId, String keyWords, Integer pageNo, Integer pageSize) {
    PageHelper.startPage(pageNo, pageSize);
    return this.mapper.selectPageList(tableId, keyWords);
  }

  public List<MetadataTableColumnVo> selectList(
      String tableId, String standardId, String keyWords) {
    return this.mapper.selectList(tableId, standardId, keyWords);
  }

  /**
   * 根据所属表id删除
   *
   * @param tableId 表id
   */
  @Transactional(rollbackFor = Exception.class)
  public void deleteColumnByTableId(String tableId) {
    if (StringUtils.isEmpty(tableId)) {
      throw new BusinessException("删除失败！传入的参数主键为null");
    }
    try {
      BdpColumnDTO bdpDasColumnDTO = new BdpColumnDTO();
      bdpDasColumnDTO.setTableId(tableId);
      delete(bdpDasColumnDTO);
    } catch (Exception e) {
      throw new BusinessException(e.getMessage(), e);
    }
  }

  /**
   * 批量添加
   *
   * @param columnList 列集合
   */
  @Transactional(rollbackFor = Exception.class)
  public void batchInsert(List<BdpColumnDTO> columnList) {
    this.mapper.batchInsert(columnList);
  }

  public int deleteByDatasourceId(String datasourceId) {
    return this.mapper.deleteByDatasourceId(datasourceId);
  }
}
